package com.tjh4.connection;

import com.mchange.v2.c3p0.*;
import org.junit.jupiter.api.Test;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @ClassName C3P0Test
 * @Description : C3P0连接池测试
 * @Author tang'jia'hao
 * @Date 2021/9/25 16:04
 * @Version 1.0
 **/
public class C3P0Test {

    // 方式一
    @Test
    public void testGetConnection() throws Exception {
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        cpds.setDriverClass( "com.mysql.cj.jdbc.Driver" ); //loads the jdbc driver
        cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/test" );
        cpds.setUser("root");
        cpds.setPassword("tjh20010228");

        cpds.setInitialPoolSize(10);

        Connection conn = cpds.getConnection();
        System.out.println("conn = " + conn);

        // 销毁连接池
        DataSources.destroy(cpds);
    }

    // 方式二,使用配置文件
    @Test
    public void testGetConnection1() throws Exception {
        ComboPooledDataSource cpds = new ComboPooledDataSource("helloc3p0");
        Connection conn = cpds.getConnection();
        System.out.println("conn = " + conn);
    }
}
